package com.mobiusws.reports.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.mobiusws.reports.action.DailyFraudDetailAction;
import com.mobiusws.reports.action.DailyFraudSummaryAction;
import com.mobiusws.reports.action.QueryDetailAction;
import com.mobiusws.reports.action.QueryFraudDetailAction;
import com.mobiusws.reports.model.CallType;
import com.mobiusws.reports.model.CallsNodup;
import com.mobiusws.reports.model.DailyFraudDetail;
import com.mobiusws.reports.model.DailyFraudSummary;
import com.mobiusws.reports.model.LoginInfo;
import com.mobiusws.reports.model.MenuItem;
import com.mobiusws.util.DateUtil;
import com.mobiusws.util.JsonUtil;
import com.mobiusws.util.MaskUtil;
import com.mobiusws.util.OperatorConstants;
import com.mobiusws.util.SqlUtil;
import com.mobiusws.util.StringUtils;
import com.mysql.jdbc.PreparedStatement;

@Repository("queryFraudDetailDao")
public class QueryFraudDetailDao {
	@Resource
	private JdbcTemplate dbxJdbc;
	@Resource
	private JdbcTemplate coreJdbc;
	@Resource
	private SqlUtil sqlUtil;
	private Logger logger = Logger.getRootLogger();

	private int cnt;
	private List<CallsNodup> list;

	private void logStmt(String sql, Object[] param) {
		logger.info(sql);
		StringBuilder logTxtBld = new StringBuilder();
		for (Object para : param) {
			logTxtBld.append(para).append(", ");
		}
		logger.info(logTxtBld.substring(0, logTxtBld.length() - 2));
	}

	private void execQuery(JdbcTemplate jdbc, String sql, Object[] param,
			BeanPropertyRowMapper rowMapper, QueryType queryType) {
		logStmt(sql, param);
		switch (queryType) {
		case INT:
			this.cnt = jdbc.queryForInt(sql, param);
			break;
		case LIST:
			this.list = jdbc.query(sql, param, rowMapper);
			break;
		}
	}
	
	private String getRealSql(String sql, QueryFraudDetailAction action) {
		String[] smsisdnArray=action.getSmsisdnArray();
		int len=smsisdnArray.length;
		StringBuilder builder=new StringBuilder();
		for(int i=0; i<len; i++) {
			builder.append("?,");
		}
		String replaceStr=builder.substring(0, builder.length()-1);
		String realSql = sql.replace(":msisdn", replaceStr);
		return realSql;
	}
	
	private Object[] getParamCnt(QueryFraudDetailAction action) {
		String[] smsisdnArray=action.getSmsisdnArray();
		int len=smsisdnArray.length;
		Object[] param=new Object[len+2];
		param[0]=action.getStartDate();
		param[1]=action.getEndDate();
		for(int i=0; i<len; i++) {
			param[i+2]=smsisdnArray[i];
		}
		return param;
	}
	
	private Object[] getParam(QueryFraudDetailAction action) {
		String[] smsisdnArray=action.getSmsisdnArray();
		int len=smsisdnArray.length;
		Object[] param=new Object[len+4];
		param[0]=action.getStartDate();
		param[1]=action.getEndDate();
		for(int i=0; i<len; i++) {
			param[i+2]=smsisdnArray[i];
		}
		param[len+2]=action.getStart();
		param[len+3]=action.getLimit();
		return param;
	}

	public List<CallsNodup> getFraudSummary(QueryFraudDetailAction action) {
		String sqlCntId="";
		String sqlId="";
		if(action.getQueryType()==1) {
			sqlCntId="getFraudSummaryByDayMSISDNCount1";
			sqlId="getFraudSummaryByDayMSISDN1";
		} else {
			sqlCntId="getFraudSummaryByDayMSISDNCount2";
			sqlId="getFraudSummaryByDayMSISDN2";
		}
		
		if (action.getStart() == 0 || action.getTotal() < 1) {
			String sqlCnt = sqlUtil.getSqlById(sqlCntId);
			String realSqlCnt=getRealSql(sqlCnt, action);
			Object[] param = getParamCnt(action);
			execQuery(dbxJdbc, realSqlCnt, param, null, QueryType.INT);
			action.setTotal(cnt);
		}
		
		if (action.getTotal() > 0) {
			String sql = sqlUtil.getSqlById(sqlId);
			String realSql=getRealSql(sql, action);
			Object[] param = getParam(action);
			BeanPropertyRowMapper rowMapper = new BeanPropertyRowMapper(
					DailyFraudSummary.class);
			execQuery(dbxJdbc, realSql, param, rowMapper, QueryType.LIST);
		} else {
			this.list = new ArrayList();
		}
		
		return this.list;
	}
}
